﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for XResourceFileEditor.xaml
    /// </summary>
    public partial class XResourceFileEditor : XPropertyValueEditor
    {
        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue", typeof(ResourceFile), typeof(XResourceFileEditor), new PropertyMetadata(propertyChangedCallback));
        private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            XResourceFileEditor editor = d as XResourceFileEditor;
            if (e.NewValue != null)
            {
                editor.ResourceFile = (ResourceFile)e.NewValue;
            }
            else
            {
                editor.ResourceFile = null;
            }
        }
        protected override void OnXInit(Xceed.Wpf.Toolkit.PropertyGrid.PropertyItem propertyItem)
        {
            Binding binding = new Binding();
            binding.Source = propertyItem;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.SetBinding(PropertyValueProperty, binding);
        }
        protected override DependencyProperty GetPropertyValue()
        {
            return PropertyValueProperty;
        }
        protected override void OnUnDo(object oldValue)
        {
            base.OnUnDo(oldValue);

            ResourceFile = (ResourceFile)oldValue;
        }

        public ResourceFile ResourceFile { get; private set; }
        public XResourceFileEditor()
        {
            InitializeComponent();
        }
        protected override void OnXInit(Part partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            Binding binding = new Binding();
            binding.Source = partInstance;
            binding.Path = new PropertyPath(propertyInfo.Name);
            this.SetBinding(PropertyValueProperty, binding);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ResourceFileEditWindow window = new ResourceFileEditWindow(ResourceFile);
            if (window.ShowDialog() == true)
            {
                ResourceFile = window.RetResourceFile;
                this.UpdateXPropertyValue(window.RetResourceFile);
            }
        }
    }
}
